package com.ccnu.sm.transaction;/*
 * @author raohj
 * @date 2019/11/1016:33
 * @description 针对数据库事务进行测试
 */

import com.ccnu.rc.date.RDate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Service
@EnableTransactionManagement
public class RTransactionManagement {
    private static final Log log = LogFactory.getLog(RTransactionManagement.class);
    @Autowired
    private DataSource dataSource;

    /*
     * 功能描述 手写事务处理
     * @author raohj
     * @date 2019/11/14
     * @descript
     * @params
     * @return
     */
    public void testBasic() {
        ObjectMapper objMapper = new ObjectMapper();
        Connection conn = DataSourceUtils.getConnection(dataSource);
        log.info("打印connection参数");
        try {
            log.info(objMapper.writeValueAsString(conn));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        //
        try {
            Statement statement = conn.createStatement();
            statement.execute("select * from t_cust");
            ResultSet resultSet = statement.getResultSet();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.commit();
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
     * 功能描述 测试事务注解
     * @author raohj
     * @date 2019/11/14
     * @descript
     * @params
     * @return
     */

    @Transactional
    public void testAnnoation() {
        // 获取数据库连接
        Connection connection = DataSourceUtils.getConnection(dataSource);
        try {
            Statement statement = connection.createStatement();
            // 获取当前最大的客户号
            statement.execute("select max(cust_no) as max_cust_no from t_cust");
            ResultSet resultSet = statement.getResultSet();
            String maxCustNo = null;
            if(!resultSet.next()){
                log.debug("没有找到最大的客户号");
                maxCustNo = RDate.getCurrentYMD();
            } else {
                maxCustNo = resultSet.getString("max_cust_no");
            }            // 最大的客户号增加1
            Integer integer = Integer.valueOf(maxCustNo);
            integer++;
            maxCustNo = integer.toString();
            statement.execute("INSERT INTO t_cust (CUST_NO, CUST_TYPE, CUST_NAME, ID_TYPE, ID_CODE, BANK_ACCT) VALUES ('" + maxCustNo.toString() + "', '1', 'rhj', '0', '421024198808282012', '62201341343151')");
            testAnnotionEx();
            // 测试另外一个事务
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 测试事务注解，开启一个新事务
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void testAnnotionEx () throws SQLException {
        // 获取数据库连接
        Connection connection = DataSourceUtils.getConnection(dataSource);
        Statement statement = connection.createStatement();
        // 获取当前最大的客户号
        statement.execute("select max(cust_no) as max_cust_no from t_cust");
        ResultSet resultSet = statement.getResultSet();
        String maxCustNo = null;
        if(!resultSet.next()){
            log.debug("没有找到最大的客户号");
            maxCustNo = RDate.getCurrentYMD();
        } else {
            maxCustNo = resultSet.getString("max_cust_no");
        }
        // 最大的客户号增加1
        Integer integer = Integer.valueOf(maxCustNo);
        integer++;
        maxCustNo = integer.toString();
        statement.execute("INSERT INTO t_cust (CUST_NO, CUST_TYPE, CUST_NAME, ID_TYPE, ID_CODE, BANK_ACCT) VALUES ('" + maxCustNo.toString() + "', '1', 'rhj', '0', '421024198808282012', '62201341343151')");
        // 测试一笔异常
        statement.execute("INSERT INTO t_cust (CUST_NO, CUST_TYPE, CUST_NAME, ID_TYPE, ID_CODE, BANK_ACCT) VALUES ('" + maxCustNo.toString() + "', '1', 'rhj', '0', '421024198808282012', '62201341343151')");
    }
}
